layui.use(['table', 'admin', 'ax','form','laydate','upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var form = layui.form;
    /**
     * 管理
     */
    var DbiShop = {
        tableId: "dbiShopTable"
    };

    /**
     * 初始化表格的列
     */
    DbiShop.initColumn = function () {
        return [[
            {field: 'code', sort: true, title: '门店编码'},
            {field: 'shopName', sort: true, title: '门店'},
            {field: 'rangeDate', sort: true, title: '时间范围'},
            {field: 'origMoney', sort: true, title: '营业额', totalRow: true}

        ]];
    };

    /**
     * 点击查询按钮
     */
    DbiShop.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        //日期维度
        queryData['dateDimension'] = $("#dateDimension").val();
        //日期参数
        queryData['rangeDate'] = $("#timecell").val();
        table.reload(DbiShop.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    DbiShop.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/business/add';
    };

    /**
     * 导出excel按钮
     */
    DbiShop.exportExcel = function () {
        var checkRows = table.checkStatus(DbiShop.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    DbiShop.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/business/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    DbiShop.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/business/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(DbiShop.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbiShop.tableId,
        url: Feng.ctxPath + '/business/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        totalRow: true,
        cols: DbiShop.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DbiShop.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        DbiShop.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        DbiShop.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + DbiShop.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DbiShop.openEditDlg(data);
        } else if (layEvent === 'delete') {
            DbiShop.onDeleteItem(data);
        }
    });

    //日期时间选择器
    laydate.render({
        elem: '#timecell',
        type: 'datetime',
        range: true,
        tiger: 'click'
    });

    //日期维度
    form.on('select(dateDimension)', function (data) {
        var dimension = data.value;
        if (dimension == "day") {

            $("#timecell").remove();

            $(".timecell").html("<input id='timecell' name='rangeDate' class='layui-input' type='text' readonly style='width: 300px' placeholder='时间范围'/>")

            initDayDate();
        } else if(dimension=='month'){
            $("#timecell").remove();

            $(".timecell").html("<input id='timecell' name='rangeDate' class='layui-input' type='text' readonly style='width: 300px' placeholder='时间范围'/>")

            initMonthDate();
        }
    });



    function initDayDate() {
        laydate.render({
            elem: '#timecell',
            type: 'datetime',
            range: true,
            tiger: 'click'
        });

    };


    function initMonthDate() {
        laydate.render({
            elem: '#timecell',
            type: 'month',
            range: true,
            tiger: 'click'
        });

    };

});
